import { getPermissions } from '@/modules/app-gateway';
import { baseAlert } from '@/modules/crud';

export const hasAuth = (auth: string | number | string[] | number[], alert = true) => {
  const map = getPermissions();
  // 没传值 返回true
  if (!auth) return true;
  let arr: any[] = [];
  if (typeof auth === 'string' || typeof auth === 'number') arr = [auth];
  const flag = arr.some((key: string | number) => map[key]);
  if (!flag && alert) {
    noAuthAlert();
  }
  return flag;
};

export const noAuthAlert = (msg?: string) => {
  return baseAlert({
    title: '错误提示',
    message: msg ?? '权限不足，无法进行操作',
    type: 'error'
  });
};

// 兼容旧写法，后面移除
export const hasPermission = hasAuth;
